package de.zainodis.kitchengnome.ui;

import java.sql.SQLException;
import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import de.zainodis.commons.LogCat;
import de.zainodis.commons.quickaction.ActionItem;
import de.zainodis.commons.quickaction.QuickAction;
import de.zainodis.commons.quickaction.QuickAction.OnActionItemClickListener;
import de.zainodis.commons.utils.StringUtils;
import de.zainodis.commons.utils.Toaster;
import de.zainodis.kitchengnome.R;
import de.zainodis.kitchengnome.domainmodel.ListItem;
import de.zainodis.kitchengnome.domainmodel.Suggestion;
import de.zainodis.kitchengnome.ui.adapter.SuggestionListAdapter;

public class EditSuggestions extends BaseActivity {

	private static final String TAG = "EditSuggestions";

	/** Quick action to delete an item */
	private final int ID_DELETE_ITEM = 0;

	/** Position of the item the user last clicked-on */
	private int selectListRow;

	/** Contains quick actions to modify items */
	private QuickAction quickActionBar;

	/** Adapter handling the list items */
	private SuggestionListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.edit_suggestions);
		setContentView(R.layout.layout_item_editor);

		// Grab the list ...
		ListView listView = (ListView) findViewById(R.id.layout_item_editor_list);
		adapter = new SuggestionListAdapter(this, getItems());
		// ... so we can attach the adapter to it
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				selectListRow = position;
				quickActionBar.show(view);
			}
		});

		// TODO consider filtering list while the user types
		createActionBar();
	}

	protected ArrayList<Suggestion> getItems() {
		try {
			// Load suggestions from the database
			return new ArrayList<Suggestion>(getHelper().getSuggestionDao().queryForAll());
		} catch (SQLException e) {
			LogCat.e(TAG, "Failed to load suggestions.", e);
		}
		// TODO let the user know that loading his data failed
		return new ArrayList<Suggestion>();
	}

	/**
	 * Subclasses may override this to decide what should happen if the user
	 * clicks
	 * 
	 * @return
	 */
	protected OnItemClickListener getOnItemClickListener() {
		return new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				selectListRow = position;
				quickActionBar.show(view);
			}
		};
	}

	protected void createActionBar() {
		quickActionBar = new QuickAction(this);
		quickActionBar.addActionItem(new ActionItem(ID_DELETE_ITEM, getString(R.string.delete), getResources()
				.getDrawable(R.drawable.delete)));

		// Setup QuickAction Bar
		quickActionBar.setOnActionItemClickListener(new OnActionItemClickListener() {
			@Override
			public void onItemClick(QuickAction source, int pos, int actionId) {
				switch (actionId) {
					case ID_DELETE_ITEM:
						getSuggestionDao().remove(adapter.delete(selectListRow));
						break;
				}
			}
		});
	}

	public void add(View originator) {
		// Check if input is valid
		TextView nameField = (TextView) findViewById(R.id.layout_inputbar_name);
		String name = nameField.getText().toString();
		if (!name.equals(StringUtils.EMPTY)) {
			add(name);
			UiUtils.clearTextView(nameField);
		} else {
			// Notify the user that only non-empty items may be added
			Toaster.shortToast(getString(R.string.name_required), this);
		}
	}

	protected void add(String name) {
		// Check if item already exists
		if (!adapter.getEntries().contains(new Suggestion(name))) {
			Suggestion newEntry = new Suggestion(name);
			adapter.add(newEntry);
			// Notify the adapter that it's dataset has changed
			adapter.notifyDataSetChanged();
			// Persist new entry to the database
			getSuggestionDao().save(newEntry);
			LogCat.i(TAG, "Added new item to the list: " + newEntry.getName());
		} else {
			Toaster.shortToast(getString(R.string.duplicated_value), this);
		}

	}

}
